package cn.com.elite.impl;

import java.util.List;
import java.util.Map;

import javax.sql.DataSource;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.dom4j.Document;
import org.dom4j.DocumentFactory;
import org.dom4j.Element;

import com.elitecrm.saas.service.EliteSAASException;

import java.sql.Types; 

public class GetPXListService{
	private static final Log log = LogFactory.getLog(GetPXListService.class);
	
	public String getPXListService(DataSource dataSource,String ep_id,Document doc,String staffid)throws Exception{
		String ReturnXml="";
		Element TypeEle=(Element)doc.selectSingleNode("/Root/Type");
		if(TypeEle!=null){
			String Type=TypeEle.getText();
			if(Type!=null){
				if(Type.equals("ks")){
					ReturnXml=getKSListServiceImpl(dataSource,ep_id,staffid);
				}else{
					throw new EliteSAASException(EliteSAASException.INVAILD_PARAMETER,"wrong type");
				}
			}else{
				throw new EliteSAASException(EliteSAASException.INVAILD_PARAMETER,"TypeEle.getText()==null");
			}
		}else{
			throw new EliteSAASException(EliteSAASException.INVAILD_PARAMETER,"TypeEle==null");
		}
		return ReturnXml;
	}
	public String getPXListServiceImpl(DataSource dataSource,String ep_id,String staffid)throws Exception{
		return null;
	}
	public String getKSListServiceImpl(DataSource dataSource,String ep_id,String staffid)throws Exception{
		String ReturnXml="";
		String W_ExamScheme="W_ExamScheme_"+ep_id;
		String W_SchemeExamRelBy="W_SchemeExamRelBy_"+ep_id;
		String sql="select "+W_ExamScheme+".SchemeID, "+W_ExamScheme+".SchemeName from "+W_SchemeExamRelBy+
				" left join "+W_ExamScheme+" on "+W_SchemeExamRelBy+".SchemeID="+W_ExamScheme+".SchemeID " +
				" where "+W_SchemeExamRelBy+".HandleBy_ID=?";
		log.debug("getKSListServiceImpl_sql:"+sql);
		Object[] params=new Object[]{staffid};  
		int[] types=new int[]{Types.VARCHAR};  
		List KSList=util.QueryPersonUseExecute(sql,types,params,dataSource);
		Document result=util.getDoc();
		Element root=result.addElement("R");
		root.addElement("result").addText("1");
		if(KSList!=null&&KSList.size()>0){
			root.addElement("KSListLEN").addText(KSList.size()+"");
	    	for(int i=0;i<KSList.size();i++){
	    		Map map=(Map)KSList.get(i);
	    		Element kslist=root.addElement("KSList");
	    		String SchemeID=(String)map.get("SchemeID");
	    		log.debug("getKSList_SchemeID:"+SchemeID);
	    		kslist.addElement("SchemeID").addText(SchemeID);
	    		String SchemeName=(String)map.get("SchemeName");
	    		log.debug("getKSList_SchemeName:"+SchemeName);
	    		kslist.addElement("SchemeName").addText(SchemeName);
	    	}
		}else{
			root.addElement("KSListLEN").addText("0");
		}
		ReturnXml=result.asXML();
		log.debug("getKSListServiceImpl_ReturnXml"+ReturnXml);
		return ReturnXml;
	}

}
